package top.infopub.mgr.hr.service.impl;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.hr.dao.StaffInfoDao;
import top.infopub.mgr.hr.dao.StaffOfficeDao;
import top.infopub.mgr.hr.domain.StaffOfficeDomain;
import top.infopub.mgr.hr.service.StaffOfficeService;
import top.infopub.mgr.hr.vo.StaffOfficeVo;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.hr.StaffOffice;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class StaffOfficeServiceImpl extends GenericServiceImpl<StaffOffice, String> implements StaffOfficeService {

    @Autowired
    private StaffOfficeDao staffOfficeDao;

    @Autowired
    private StaffInfoDao staffInfoDao;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Override
    public GenericDao<StaffOffice, String> getDao() {
        return staffOfficeDao;
    }

    @Override
    public List<StaffOfficeDomain> selectStaffValidOrgDept(String staffId) {
        return staffOfficeDao.selectStaffValidOrgDept(staffId);
    }

    @Override
    public Page<StaffOfficeVo> selectPageByDomainWhere(Page<StaffOfficeVo> page,
                                                       StaffOfficeVo staffOfficeVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<StaffOfficeVo>)staffOfficeDao.selectByDomainWhere(staffOfficeVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveStaffInfo(StaffOfficeVo staffOfficeVo) {
        if (StringUtils.isEmpty(staffOfficeVo.getStaffId())) {
            // 执行新增
            StaffInfo staffInfo = new StaffInfo();
            staffInfo.setStaffName(staffOfficeVo.getStaffName());
            staffInfo.setPhone(staffOfficeVo.getStaffPhone());
            String staffId = IdFactory.getUUIDUpper();//人员id
            staffInfo.setId(staffId);
            String staffCode = codeGeneratorConfigService.generatorCode("hr_staff_info");
            staffInfo.setStaffCode(staffCode);
            staffInfo.setState(1);//默认在职
            //新增人员
            staffInfo.setAddBy(SessionUserUtils.getSessionUserId());
            staffInfo.setAddTime(DateUtils.getCurrentDateTime());
            staffInfo.setUptBy(SessionUserUtils.getSessionUserId());
            staffInfo.setUptTime(DateUtils.getCurrentDateTime());
            staffInfoDao.insertById(staffInfo);

            //新增就职记录
            staffOfficeVo.setStaffId(staffId);
            staffOfficeVo.setShowIdx(1);
            staffOfficeVo.setStartTime(DateUtils.getCurrentDateTime());
            staffOfficeVo.setAddBy(SessionUserUtils.getSessionUserId());
            staffOfficeVo.setAddTime(DateUtils.getCurrentDateTime());
            staffOfficeVo.setUptBy(SessionUserUtils.getSessionUserId());
            staffOfficeVo.setUptTime(DateUtils.getCurrentDateTime());
            staffOfficeDao.insert(staffOfficeVo);
        }
        else {
            StaffInfo staffInfo = new StaffInfo();
            staffInfo.setId(staffOfficeVo.getStaffId());
            staffInfo.setStaffName(staffOfficeVo.getStaffName());
            staffInfo.setPhone(staffOfficeVo.getStaffPhone());
            staffInfo.setUptBy(SessionUserUtils.getSessionUserId());
            staffInfo.setUptTime(DateUtils.getCurrentDateTime());
            staffInfoDao.updateByIdSelective(staffInfo);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveStaffOffice(StaffOfficeVo staffOfficeVo) {
        // 执行新增
        if (StringUtils.isEmpty(staffOfficeVo.getId())) {
            //查询当前人员有几个岗位
            Integer maxShowIdx = staffOfficeDao.selectMaxShowIdxByStaffId(staffOfficeVo.getStaffId());
            if (maxShowIdx != null) {
                staffOfficeVo.setShowIdx(maxShowIdx + 1);
            }
            else {
                staffOfficeVo.setShowIdx(1);
            }
            //新增就职记录
            staffOfficeVo.setAddBy(SessionUserUtils.getSessionUserId());
            staffOfficeVo.setAddTime(DateUtils.getCurrentDateTime());
            staffOfficeVo.setUptBy(SessionUserUtils.getSessionUserId());
            staffOfficeVo.setUptTime(DateUtils.getCurrentDateTime());
            staffOfficeDao.insert(staffOfficeVo);

        }
        else {// 执行修改
              //staffInfo.setUptBy(SessionUserUtils.getSessionUserId());
              //staffInfo.setUptTime(DateUtils.getCurrentDateTime());
              //staffInfoDao.updateByIdSelective(staffInfo);

            StaffOffice staffOfficeUpdate = staffOfficeDao.selectByPrimaryKey(staffOfficeVo.getId());
            staffOfficeVo.setShowIdx(staffOfficeUpdate.getShowIdx());
            staffOfficeVo.setAddBy(staffOfficeUpdate.getAddBy());
            staffOfficeVo.setAddTime(staffOfficeUpdate.getAddTime());
            BeanUtils.copyProperties(staffOfficeVo, staffOfficeUpdate);
            staffOfficeUpdate.setUptTime(DateUtils.getCurrentDateTime());
            staffOfficeUpdate.setUptBy(SessionUserUtils.getSessionUserId());
            this.updateByPrimaryKey(staffOfficeUpdate);
        }

    }

    @Override
    public List<StaffOfficeDomain> selectValidStaffByDept(String deptId) {
        return staffOfficeDao.selectValidStaffByDept(deptId);
    }

    @Override
    public List<StaffOfficeVo> selectByOrgId(String orgId) {
        return staffOfficeDao.selecyByOrgId(orgId);
    }

    @Override
    public List<StaffOfficeVo> selectActiveByOrgId(String orgId) {
        return staffOfficeDao.selectActiveByOrgId(orgId);
    }

    @Override
    public List<StaffOfficeDomain> selectStaffByPostName(StaffOfficeDomain where) {
        return staffOfficeDao.selectStaffByPostName(where);
    }

}